﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Easily
{
    /// <summary>
    /// 说明：本窗体为SPI配置窗体
    /// </summary>
    public partial class frmSPISend : Form
    {
        private GeneralIcon MyIcon = null;  //保存传入的控件对象
        /// <summary>
        ///构造函数
        ///主要功能：加载保存的控件设置；生成初始代码；
        /// </summary>
        public frmSPISend(GeneralIcon icon)
        {
            InitializeComponent();
            this.MyIcon = icon;
            //添加头文件
            this.MyIcon.code.HeadFile = "#include \"spi.h\"";
            //添加可供选择的系统变量到cBox_var中
            if (Globaldefine.FrmParent.ActivedrmGraphicFlow != null)
            {
                //全局变量
                for (int i = 0; i < Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableSys.Count; i++)
                {
                    cBox_var.Items.Add(Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableSys[i]);
                }
                //传感器局部变量
                foreach (var item in Globaldefine.FrmParent.ActivedrmGraphicFlow.m_Sensor_VariableLocal.Values)
                {
                    cBox_var.Items.Add(item);
                }
                //局部变量
                foreach (var item in Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableLocal)
                {
                    cBox_var.Items.Add(item);
                }
            }

            //默认选中列表的第1个数据
            //模块号
            if (cBox_spi.Items.Count > 0)
                cBox_spi.SelectedIndex = 0;
            //主从机选择
            if (cBox_mode.Items.Count > 0)
                cBox_mode.SelectedIndex = 0;         
            //波特率
            if (cBox_baud.Items.Count > 0)
                cBox_baud.SelectedIndex = 0;
            //相位
            if (cBox_cpha.Items.Count > 0)
                cBox_cpha.SelectedIndex = 0;
            //极性
            if (cBox_cpol.Items.Count > 0)
                cBox_cpol.SelectedIndex = 0;
            //载入保存的参数
            if (MyIcon.code.Save != "")
            {
                //保存的参数共有7项，以英文分号';'分开
                //保存的内容分别是：（1）spi模块号：SPI_0,SPI_1;
                //（2）主从机选择：主机/从机（3）相位选择：0/1；
                //（4）极性选择：0/1；（
                string[] tmp = MyIcon.code.Save.Split(';');
                //spi模块号
                if (tmp[0] == "SPI_0")
                {
                    cBox_spi.SelectedIndex = 0;
                }
                else if (tmp[0] == "SPI_1")
                {
                    cBox_spi.SelectedIndex = 1;
                }
                //主从机选择
                if (tmp[1] == "主机")
                {
                    cBox_mode.SelectedIndex = 0;
                }
                else if (tmp[1] == "从机")
                {
                    cBox_mode.SelectedIndex = 1;
                }
                //波特率
                cBox_baud.SelectedItem = tmp[2];    
                //相位
                cBox_cpha.SelectedItem = tmp[3];
                //极性
                cBox_cpol.SelectedItem = tmp[4];
                //发送变量
                if (!cBox_var.Items.Contains(tmp[5]))  //防止item尚未被添加
                    cBox_var.Items.Add(tmp[5]);
                cBox_var.SelectedItem = tmp[5];             
            }
            btn_confirm_Click(new object(), new EventArgs());//触发确认
        }
     
        
        /// <summary>
        /// 点击“确定”按钮
        /// 主要功能：根据配置生成新代码，并设置对话返回为OK
        /// </summary>
        private void btn_confirm_Click(object sender, EventArgs e)
        {
            MyIcon.code.Save = "";
            string modeSelect="";
            if (cBox_mode.SelectedItem.ToString()=="主机")
            {
                modeSelect = "0";
            }
            else if (cBox_mode.SelectedItem.ToString()=="从机")
            {
                modeSelect = "1";
            }
            //初始化代码
            MyIcon.code.Initialization = "spi_init(" + cBox_spi.SelectedItem.ToString()
                + "," + modeSelect + "," +cBox_baud.SelectedItem.ToString() +"," + 
                cBox_cpha.SelectedItem.ToString() + ","+ cBox_cpol.SelectedItem.ToString() +");//初始化spi模块";
            //插入代码
            if (rBtn_send.Checked == true)
            {
                if (cBox_var.SelectedItem != null && cBox_var.SelectedItem.ToString() != "")
                {
                    string vari = cBox_var.SelectedItem.ToString();
                    //如果发送不是数组变量
                    if (vari[0] != 'g')
                        MyIcon.code.InsertCode += "spi_send1(" + cBox_spi.SelectedItem.ToString() + ","
                            + vari + ");  //发送数据\r\n";
                    else
                    {
                        string length = "";
                        int index = 0;
                        //获取数组长度
                        for (index = 1; index < vari.Length; index++)
                        {
                            if (!Char.IsDigit(vari, index))
                                break;
                        }
                        length = vari.Substring(1, index - 1);

                        MyIcon.code.InsertCode += "spi_sendN(" + cBox_spi.SelectedItem.ToString()
                            + "," + length + "," + vari + ");//发送数据\r\n";
                    }
                }
                string temp = "";
                if (cBox_var.SelectedItem != null)
                    temp = cBox_var.SelectedItem.ToString();
                MyIcon.code.Comment = "通过SPI" + cBox_spi.SelectedItem.ToString() + "发送数据：" + temp;
                   
                //保存代码
                MyIcon.code.Save = cBox_spi.SelectedItem.ToString() + ";" + cBox_mode.SelectedItem.ToString() + ";"
                + cBox_baud.SelectedItem.ToString() + ";" + cBox_cpha.SelectedItem.ToString() + ";"
                + cBox_cpol.SelectedItem.ToString() + ";"+ temp;
            }
            else    //未发送数据
            {
                //保存代码 如果没有发送任何变量则保存代码时变量保存为空
                MyIcon.code.Save = cBox_spi.SelectedItem.ToString() + ";" + cBox_mode.SelectedItem.ToString() + ";"
                + cBox_baud.SelectedItem.ToString() + ";" + cBox_cpha.SelectedItem.ToString() + ";"
                + cBox_cpol.SelectedItem.ToString()+";"+"";
            }                              
            //外部会对此进行判断，因此是必要的
            this.DialogResult = DialogResult.OK;

        }
        /// <summary>
        /// 单击“取消”按钮
        /// 主要功能：设置返回结果为取消
        /// </summary>
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
        /// <summary>
        /// 点击“注释说明”按钮
        /// 功能概要：为控件添加自定义注释说明
        /// </summary>
        private void btn_comment_Click(object sender, EventArgs e)
        {
            using (frmNodeEditer NE = new frmNodeEditer(this.MyIcon.code.Comment))
            {
                if (NE.ShowDialog() == DialogResult.OK)
                    this.MyIcon.code.Comment = NE.NoteEdit;
            }
        }
    }
}
